<?php

class ViewController extends Zend_Controller_Action
{
	public function init()
	{
		$title ="view an object";
		$this->view->__set("headtitle", $title);
		
// 		$script = "$(function() {
// 				  // Setup drop down menu
// 				  $('.dropdown-toggle').dropdown();
				 
// 				  // Fix input element click problem
// 				  $('.dropdown textarea, .dropdown label').click(function(e) {
// 				    e.stopPropagation();
// 				  });
// 				});";
			
		//$this->view->headScript()->appendScript($script, $type = 'text/javascript');

	}

	


	public function indexAction()
	{
		$title ="view";
		$this->view->__set("headtitle", $title);
		$helperUrl = new Zend_View_Helper_Url ( );
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");

		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
		try {
			$form = new Application_Form_Message('message');
			if(Application_Model_Enum_ClassName::exist($class) != null){
				$class = Application_Model_Enum_ClassName::exist($class);
				$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
					
				if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
				{
					$whereObject = array('id_object = ?' => $object->getObjectId());
					$offerMapper = new Application_Model_OfferMapper();
					$offer = $offerMapper->findWhere($whereObject);
					//verify if the user connected is the owner of the object
					
					$userConnected = new Application_Model_User();
					$userMapper = new Application_Model_UserMapper();
					$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
					$objectOwner = $offer->getUser();
					if($offer != null){
						$whereOffer = array('id_offer = ?' => $offer->getOfferId());
						
							if($offerMapper->isEmptyOffer($whereOffer))
							{
								if($userConnected == $objectOwner )
								{
									$this->view->activeOffer = true;
									$getObjectId = 'get'.$class.'Id';  
									$this->view->saleLink  = $helperUrl->url ( array(strtolower($class) => $object->$getObjectId().'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) ,'controller' => 'offer', 'action' => 'sale') , 'default', true);
									$this->view->rentLink  = $helperUrl->url ( array(strtolower($class) => $object->$getObjectId().'-'.Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) ,'controller' => 'offer', 'action' => 'rent') , 'default', true);
									$this->view->object = $object;
								}
								else{
									$this->view->alert = 'cet object ne vous appartient pas';
									$this->view->alertType = 'error';
								}
							}
							else{
								if($this->_request->isPost()){
									$formData = $this->getRequest()->getPost();
									if($form->isValid($formData))
									{
										$baseurl = $this->getFrontController()->getBaseUrl();
										//Send a message to the owner
										$smtpConnection = new Zend_Mail_Transport_Smtp('smtp.gmail.com', Zend_Registry::get('Mail_Config'));
										$mail = new Zend_Mail('utf-8');
										$mail	->setFrom($userConnected->getEmail(), 'Youtajeer.com')
												->addTo($objectOwner->getEmail(), 'destinataire')
												->setSubject('['.$object->getName().'] '.$userConnected->getFirstName().' '.$userConnected->getLastName().' vous a écrit')
												->setBodyHtml(	'
<table class="ecxouter" width="100%" align="center" cellspacing="0" cellpadding="0" border="0" style="background:#ddd;">
<tbody>
	<tr>
	<td>
		<table cellpadding="10" align="center" width="720" cellspacing="0" width="100%" style="border:1px solid #d9d9d9;border-radius:8px;background:#FFF;">
		<tbody>
			<tr>
			<td class="ecxt-i" valign="top" width="50"> <a class="" href="http://'.$_SERVER['HTTP_HOST'].$baseurl.'/profil/'.$userConnected->getUserId().'" style="border:none;color:#0084b4;text-decoration:none;" target="_blank"><img src="http://'.$_SERVER['HTTP_HOST'].$baseurl.'/img/profil/'.$userConnected->getAvatar().'" style="border:0;background-color:#f2f2f2;border-radius:5px;padding:0;display:block;" border="0" height="50" width="50"></a></td>
			<td class="ecxt-m" valign="top">
				<table align="left" border="0" cellpadding="0" cellspacing="0" width="85%">
				<tbody>
					<tr>
					<td> <p class="ecxt-t" style="font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;color:#333;font-size:14px;line-height:1.1;font-weight:bold;"><a class="ecxt-n" href="'.$_SERVER['HTTP_HOST'].$baseurl.'/profil/'.$userConnected->getUserId().'" style="border:none;color:#0084b4;text-decoration:none;color:#333;" target="_blank"><span dir="ltr">'.$userConnected->getFirstName().' '.$userConnected->getLastName().'</span></a>
						 vous a écrit à propos de 
						<a class="" target="_blank" style="border:none;color:#0084b4;text-decoration:none;" href="http://'.$_SERVER['HTTP_HOST'].$baseurl.'/view/'.strtolower($offer->getClasse()).'/'.$id_object.'-'.$name.'">
					      '.$object->getName().'
					    </a>
						 </p>
					<div class="ecxt-s">
					<p style="font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;color:#333;font-size:14px;line-height:1.25em;">'.$form->getValue('message').
					'
					</div> 
					
					</td>
					</tr>
				</tbody>
				</table>
				<table class="ecxdate" style="font-size:12px;width:60px;text-align:right;" align="right" border="0" cellpadding="0" cellspacing="0" width="40">
				<tbody>
					<tr>
					<td><a href="http://'.$_SERVER['HTTP_HOST'].$baseurl.'/view/'.strtolower($offer->getClasse()).'/'.$id_object.'-'.$name.'" style="border:none;color:#0084b4;text-decoration:none;font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;color:#999;" target="_blank">'.date("Y-m-d").'</a></td>
					</tr>
				</tbody>
				</table> 
			</td>
			</tr>
		</tbody>
		</table>
		<table class="ecxaction" width="720" cellspacing="0" cellpadding="0" border="0" align="center">
		<tbody>
		<tr>
		<td class="ecxmid ecxo-m" style="padding:10px;font-family:\'Helvetica Neue\', Helvetica, Arial, sans-serif;color:#333;padding:0;border-radius:5px;background-color:#ededed;padding:10px;">
			<table class="ecxo-c" border="0" cellpadding="0" cellspacing="0" width="100%">
			<tbody>
				<tr>
				<td class="ecxcut ecxsign-message" style="line-height:15px;"> <span class="ecxsuggestions_tip" style="font-size:14px;color:#333;font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;"><strong>Ne ratez rien.</strong> Restez au courant de ce qui se passe.</span></td>
				<td align="right" valign="middle">
					<table class="button ecxmedia_button" style="background-color:#33a9e5;background-position:top;border-radius:5px;border-color:#2288cc;border-style:solid;border-width:1px;text-align:center;height:28px;" bgcolor="#33a9e5" border="0" cellpadding="0" cellspacing="0">
					<tbody>
					<tr>
						<td class="spacer" style="font-size:1px;" width="10"></td>
						<td align="center" height="28"> <span class="ecxbutton_text" style="color:#fff;font-size:13px;font-weight:bold;text-shadow:0 -1px 0 #333;white-space:nowrap;font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;"><a class="ecxbutton_link" href="http://'.$_SERVER['HTTP_HOST'].$baseurl.'" style="border:none;color:#0084b4;text-decoration:none;color:#fff;" target="_blank">Aller sur Youtajeer.com</a></span></td>
						<td class="spacer" style="font-size:1px;" width="10"></td>
					</tr>
					</tbody>
					</table> 
				</td>
				</tr>
			</tbody>
			</table>
		</td>
		</tr>
		</tbody>
		</table>
	</td>
</tr>
</tbody>
</table>

														'
														);
										$mail->send($smtpConnection);
										
										$this->view->alert = 'Your message was sent successfuly';
										$this->view->alertType = 'success';
										$form->reset();
									}
									else{
										$this->view->alert = 'Your message was not be sent';
										$this->view->alertType = 'error';
									}
								}
								$this->view->isRent = false;
								$offerView = $offerMapper->offerBuilder($whereOffer);
								$offerType = explode("_",get_class($offerView));
								$this->view->offerType = $offerType[2];
								if ($offerView instanceof Application_Model_Rent) {
									$this->view->isRent = true;
								}
								$addresses = $offerView->getAddresses();
								$address = $addresses[0];
								
								$this->view->address = $address;
								$this->view->offer = $offerView;
								$this->view->object = $object;
								//$this->view->error = 'cet object ';
							}
							$title =$object->getName();
							$this->view->__set("headtitle", $title);
						
					}
					$this->view->objectOwner = $objectOwner;
					$this->view->form = $form;
					
					
				}else{
					$this->view->alert = 'l\'object n\'existe pas';
					$this->view->alertType = 'error';
					$this->view->object = null;
				}

			}else{
				$this->render('error');
			}
				
		}catch (Exception $e) {
			//echo 'Exception reçue : ',  $e->getMessage(), "\n";
			$this->view->alert = $e->getMessage();
			$this->view->alertType = 'error';

		}
		}else{
				
			$this->_helper->redirector ( 'index', 'index' );
		}


		$this->_helper->viewRenderer->setScriptAction(strtolower($class));



	}
	
	public function carAction()
	{
		$car = $this->_request->getParams();
	}
	
	//@TODO to delete
	public function loctreeAction()
	{
		
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			$loctree = new Application_Model_LocTree();
			$loctreeMapper = new Application_Model_LocTreeMapper();
			$entries = $loctreeMapper->fetchAll();
// 			$tree = array();
// 			$loctreeMapper->constructTree($entry = new Application_Model_LocTree(),array('id_parent = ?' => 0), $tree);
// 			var_dump($tree);
			$this->view->list = $entries;
			
			$form = new Application_Form_LocTree('loginform');
			$this->view->form = $form;
			
			if ($this->_request->isPost()) {
				$formData = $this->_request->getPost();
				if($form->isValid($formData))
				{
					$loctree->setOptions($form->getValues());
					var_dump($loctree);
					$loctreeMapper->save($loctree);
				}
				$this->_helper->redirector ( 'loctree', 'view' );
			}
			
			
			
			
		}else{
				
			$this->_helper->redirector ( 'index', 'index' );
		}
		
	}
	
}